package com.franklin.java.action.sql.entity;

import com.franklin.java.action.sql.AbstractSqlAction;
import com.franklin.java.action.sql.entity.ui.GenerateEntityClassForm;
import com.franklin.java.api.ui.jpanel.MyJScrollPane;
import com.franklin.java.icon.PluginIcons;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.popup.*;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

/**
 * 根据Sql生成实体类Action
 * @author Franklin
 * @since 2021/9/10 18:17
 */
public class GenerateEntityClassAction extends AbstractSqlAction {

    public GenerateEntityClassAction(PsiElement sqlActionElement) {
        super(
            "Generate an entity class for this sql",
            "Helps you easier to create an entity for complicated sql",
            AllIcons.Nodes.Class,
            sqlActionElement
        );


    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        JBPopupFactory jbPopupFactory = JBPopupFactory.getInstance();

        GenerateEntityClassForm generateEntityClassForm = new GenerateEntityClassForm(resolveSql2Columns(), sqlActionElement,this);
        MyJScrollPane mainPanel = generateEntityClassForm.getMainPanel();
        ComponentPopupBuilder componentPopupBuilder = jbPopupFactory.createComponentPopupBuilder(mainPanel, mainPanel);
        JBPopup generateClassPopup = componentPopupBuilder.setTitle("Generate Entity Class")
            .setCancelKeyEnabled(true)
            .setCancelButton(new IconButton("close", AllIcons.Actions.CloseHovered))
            .setTitleIcon(new ActiveIcon(PluginIcons.SERVICE))
            .setMovable(true)
            .setShowShadow(true)
            .setResizable(true)
            .setRequestFocus(true)
            .setCancelOnClickOutside(false)
            .createPopup();
        generateClassPopup.setSize(new Dimension(1170,800));

        generateEntityClassForm.getCancelBtn().addActionListener(
            actionEvent -> generateClassPopup.cancel()
        );

        generateClassPopup.showCenteredInCurrentWindow(sqlActionElement.getProject());

    }

}
